#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Test_PcanSignal.py
@Time    :   2024/05/17 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pickle import FALSE, TRUE
from pytest_assume.plugin import assume
 
import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

import project_CAN
import Set_up


class Test_EraseMemory(object):

    @allure.severity('normal')
    @allure.story('Erase DID no lock test')
    @allure.description('Test Erase DID no lock in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_Erase_DID_noLock_Phy(self,action,Driver):      
        iStep = 1

        # 擦除DID区域
        iStep, respStr = Driver['CAN'].EraseDID(Driver, iStep, 0)

        with assume: assert respStr == 'Test Erase DID lock'

    @allure.severity('normal')
    @allure.story('BYD Erase_DID no lock test')
    @allure.description('Test BYD Erase_DID no lock in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_Erase_Backup_DID_noLock_Phy(self,action,Driver):      
        iStep = 1

        # 擦除DID备份区域
        iStep, respStr = Driver['CAN'].EraseDID(Driver, iStep, 1)

        with assume: assert respStr == 'Test Erase Backup DID lock'

    
    @allure.severity('normal')
    @allure.story('Erase DID with power off and restart test')
    @allure.description('Test Erase DID with power off and restart in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_Erase_DID_WithPower_Phy(self,action,Driver):      
        iStep = 1
        # 写VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17', '6E F1 90', 3, iStep,True, 'UDS', 'noClearFault')
        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')
        # 擦除DID区域
        iStep, respStr = Driver['CAN'].EraseDID(Driver, iStep, 0)
        #判断擦除是否成功
        with assume: assert respStr == 'Test Erase DID OK'

        # 重启产品
        iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)

        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')


    @allure.severity('normal')
    @allure.story('Erase DID with 11 off and restart test')
    @allure.description('Test Erase DID with 11 off and restart in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_Erase_DID_With11_Phy(self,action,Driver):      
        iStep = 1
        # 写VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17', '6E F1 90', 3, iStep,True, 'UDS', 'noClearFault')
        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')
        # 擦除DID区域
        iStep, respStr = Driver['CAN'].EraseDID(Driver, iStep, 0)
        #判断擦除是否成功
        with assume: assert respStr == 'Test Erase DID OK'

        # 重启产品
        iStep = Driver['CAN'].restartMRR_11(Driver,iStep)

        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')
    
    @allure.severity('normal')
    @allure.story('Erase backup DID with power off and restart test')
    @allure.description('Test Erase backup DID with power off and restart in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_Erase_BackupDID_WithPower_Phy(self,action,Driver):      
        iStep = 1
        # 写VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17', '6E F1 90', 3, iStep,True, 'UDS', 'noClearFault')
        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')
        # 擦除备份DID区域
        iStep, respStr = Driver['CAN'].EraseDID(Driver, iStep, 1)
        #判断擦除是否成功
        with assume: assert respStr == 'Test Erase Backup DID OK'
        #判断擦除是否成功
        with assume: assert respStr == 'Test Erase Backup DID OK'

        # 重启产品
        iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)

        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')
        # 写VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep, True, 'UDS', 'noClearFault')
        # 擦除DID区域
        iStep, respStr = Driver['CAN'].EraseDID(Driver, iStep, 0)
        # 重启产品
        iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')

    
    @allure.severity('normal')
    @allure.story('Erase backup DID with 11 off and restart test')
    @allure.description('Test Erase backup DID with 11 off and restart in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_Erase_BackupDID_With11_Phy(self,action,Driver):      
        iStep = 1
        # 写VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17', '6E F1 90', 3, iStep,True, 'UDS', 'noClearFault')
        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')
        # 擦除备份DID区域
        iStep, respStr = Driver['CAN'].EraseDID(Driver, iStep, 1)
        #判断擦除是否成功
        with assume: assert respStr == 'Test Erase Backup DID OK'

        # 重启产品
        iStep = Driver['CAN'].restartMRR_11(Driver,iStep)

        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')
        # 写VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep, True, 'UDS', 'noClearFault')
        # 擦除DID区域
        iStep, respStr = Driver['CAN'].EraseDID(Driver, iStep, 0)
        # 重启产品
        iStep = Driver['CAN'].restartMRR_11(Driver,iStep)
        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')
    
    @allure.severity('normal')
    @allure.story('Erase DID and backup DID with power off and restart test')
    @allure.description('Test Erase DID and backup DID with power off and restart in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_Erase_DID_And_Backup_WithPower_Phy(self,action,Driver):      
        iStep = 1
        # 写VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '2E F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17', '6E F1 90', 3, iStep,True, 'UDS', 'noClearFault')
        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17", 0, iStep, False, 'UDS', 'noClearFault')
        # 擦除DID区域
        iStep, respStr = Driver['CAN'].EraseDID(Driver, iStep, 0)
        #判断擦除是否成功
        with assume: assert respStr == 'Test Erase DID OK'
        # 擦除备份DID区域
        iStep, respStr = Driver['CAN'].EraseDID(Driver, iStep, 1)
        #判断擦除是否成功
        with assume: assert respStr == 'Test Erase Backup DID OK'
        # 重启产品
        iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
        # 读取VIN
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 F1 90", "62 F1 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00", 0, iStep, False, 'UDS', 'noClearFault')
